Advertisement
RobiSydney

Arduino Uno GPS + Dimming + LCD.ino

May 24th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.36 KB | None | 0 0
  1.  
  2. /**
  3.    Code to display the time & date from a GPS receiver on a LCD.
  4.  
  5.    This code was inspired by http://arduino.cc/en/Tutorial/LiquidCrystal and
  6.    http://playground.arduino.cc/Tutorials/GPS
  7.  
  8.    For more information, see http://quaxio.com/arduino_gps/
  9.  
  10. Edit: 4 June 2017 : Fixed the direction logic..  North is OR less than 22.5 OR greater than 3sumptin || everything else is AND &&
  11. Cleaned it up a bit.
  12.  
  13. */
  14. #include <SoftwareSerial.h>
  15. #include <LiquidCrystal.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <math.h>
  19. #define DEBUG 0
  20. SoftwareSerial mySerial(19, 18); // RX, TX
  21. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  22. int VOID = 1;
  23. int rxPin = 19; // RX pin
  24. int txPin = 18; // TX pin
  25. int DayLight = -8;
  26. int byteGPS = -1;
  27. int counter1 = 0; // counts how many bytes were received (max 300)
  28. int counter2 = 0; // counts how many commas were seen
  29. int offsets[13];
  30. char buf[310] = "";
  31. int first = 1;
  32. byte North[8] = {
  33.   B00000,
  34.   B00100,
  35.   B01110,
  36.   B10101,
  37.   B00100,
  38.   B00100,
  39.   B00100,
  40.   B00000
  41.  
  42. };
  43.  
  44. byte NorthEast[8] = {
  45.   B00000,
  46.   B00000,
  47.   B01111,
  48.   B00011,
  49.   B00101,
  50.   B01001,
  51.   B10000,
  52.   B00000
  53.  
  54. };
  55. byte East[8] = {
  56.   B00000,
  57.   B00100,
  58.   B00010,
  59.   B11111,
  60.   B00010,
  61.   B00100,
  62.   B00000,
  63.   B00000
  64.  
  65. };
  66. byte SouthEast[8] = {
  67.   B00000,
  68.   B10000,
  69.   B01001,
  70.   B00101,
  71.   B00011,
  72.   B01111,
  73.   B00000,
  74.   B00000
  75.  
  76. };
  77. byte South[8] = {
  78.   B00000,
  79.   B00100,
  80.   B00100,
  81.   B00100,
  82.   B10101,
  83.   B01110,
  84.   B00100,
  85.   B00000
  86.  
  87. };
  88. byte SouthWest[8] = {
  89.   B00000,
  90.   B00001,
  91.   B10010,
  92.   B10100,
  93.   B11000,
  94.   B11110,
  95.   B00000,
  96.   B00000
  97.  
  98. };
  99. byte West[8] = {
  100.   B00000,
  101.   B00100,
  102.   B01000,
  103.   B11111,
  104.   B01000,
  105.   B00100,
  106.   B00000,
  107.   B00000
  108.  
  109. };
  110. byte NorthWest[8] = {
  111.   B00000,
  112.   B00000,
  113.   B11110,
  114.   B11000,
  115.   B10100,
  116.   B10010,
  117.   B00001,
  118.   B00000
  119.  
  120. };
  121. // Keys AnalogRead(0);
  122. const int Keyselect = 650;
  123. const int KeyLeft = 409;
  124. const int KeyRight = 49;
  125. const int KeyUp = 100;
  126. const int KeyDown = 255;
  127. // select 600-650
  128. // left 350-500
  129. // right 0
  130. // up 50-200
  131. // down 201-349
  132.  
  133. int Key, KeyCount, LastKey;
  134. int backLight=125;
  135. int BackLightPin = 10;
  136. char CRLF[3] = {13,10,0};
  137.  
  138. char chksum(char checksumsource[300], char header[1], char footer[1]) {
  139.   int CRC=0;
  140.   int i,h,f = 0;
  141.   if(checksumsource[i] == header[0]) h++;
  142.  
  143. #if DEBUG == 5
  144. Serial.println("\n -=-=-=-=-=-=-=-=-= CHK SUM SOURCE =-=-=-=-=-=-=-=-");  
  145. Serial.print(checksumsource);
  146. Serial.println("\n -=-=-=-=-=-=-=-=-= chksum =-=-=-=-=-=-=-=-");  
  147. Serial.print(checksumsource[0]);
  148. #endif  
  149.   for(i=h; checksumsource[i] != '*'; i++ ) {
  150. #if DEBUG == 1
  151.   Serial.print(checksumsource[i]);
  152. #endif
  153.     CRC = (CRC ^ checksumsource[i]) && 255;    
  154.   }
  155.   f=i;
  156. #if DEBUG == 5
  157.   Serial.print(checksumsource[f]);
  158.   Serial.print(CRC,HEX);
  159.   Serial.print("\n -=-=-=-=-=-=-=-= end chksum =-=-=-=-=-=-=-\n\n");  
  160. #endif
  161.   return CRC;
  162. }
  163.  
  164. /**
  165.    Setup display and gps
  166. */
  167. void setup() {
  168.   char buf2(300);
  169.   pinMode(rxPin, INPUT);
  170.   pinMode(txPin, OUTPUT);
  171.   pinMode(10,OUTPUT);
  172.   Serial.begin(9600);
  173.   lcd.createChar(0,North);
  174.   lcd.createChar(1,NorthEast);
  175.   lcd.createChar(2,East);
  176.   lcd.createChar(3,SouthEast);
  177.   lcd.createChar(4,South);
  178.   lcd.createChar(5,SouthWest);
  179.   lcd.createChar(6,West);
  180.   lcd.createChar(7,NorthWest);
  181.  
  182.   lcd.begin(16, 2);
  183.   lcd.clear();
  184.   //Serial.println("Wait for gps");
  185.   lcd.print("    Wait for gps");
  186.  
  187.   offsets[0] = 0;
  188.   reset();
  189.   mySerial.begin(9600);
  190.   //mySerial.println(cmd);
  191. #if DEBUG == 1  
  192.   char buf4[]="$PSRF103,00,00,01,01*11";
  193.   delay(1000);
  194.   //Serial.print("\n");
  195.   //Serial.print(buf4);
  196.   //Serial.print("-");
  197.   //Serial.print(chksum(buf4,'$','*'),HEX);
  198.   //Serial.print(CRLF);
  199. #endif
  200. }
  201.  
  202.  
  203. void reset() {
  204.   counter1 = 0;
  205.   counter2 = 0;
  206.   offsets[0] = 0;
  207.   //  offset=0;
  208. }
  209.  
  210. int get_size(int offset) {
  211.   return offsets[offset + 1] - offsets[offset] - 1;
  212. }
  213.  
  214. int DoGPRMC(void ) {
  215.   int b,j,h = 0;
  216.   float f;
  217.   char cmd[7] = "$GPRMC";
  218.   char buf2[11];
  219.   char buf3[11];
  220.   if (counter2 == 0) return 0;
  221.   if ((counter2 == 12 ) && (get_size(0) == 6)) {
  222.     // Check that we received $GPRMC
  223.     for (j = 0; j < 6; j++) {
  224.       if (buf[j] != cmd[j]) {
  225.         return 0;
  226.       }
  227.     }
  228.   //Serial.println("\n ---------- GPRMC checksum --------------- ");
  229.   for (j = 1; buf[ j-1] != '*'; ++j) {
  230.     buf2[j] = buf[ j];
  231.   }
  232.   buf2[0] = '$';
  233.   buf2[j]=0;
  234.    
  235.     if (get_size(1) != 10) {
  236.       return 0;
  237.     }
  238.  
  239.     // Check that date is well formed
  240.     if (get_size(9) != 6) {
  241.       return 0;
  242.     }
  243.     if (first == 1 ) {
  244.       first = 0;
  245.       lcd.clear();
  246.     }
  247.  
  248.     // TODO: compute and validate checksum
  249.  
  250.     // TODO: handle timezone offset
  251. // 0      1          2 3         4 5          6 7    8     9      10 11 12
  252. // $GPRMC,185726.000,A,3327.8968,N,11707.0896,W,0.00,44.99,060517,  ,  ,A*4B
  253.  
  254.     // print time
  255.     lcd.setCursor(11, 0);
  256.     for (j = 0; j < 2; j++) {
  257.       buf2[j] = buf[offsets[1] + j];
  258.     }
  259.     buf2[j] = 0;
  260.    
  261.     DayLight = -8;
  262.     if(buf[offsets[9]]='0' && ( buf[offsets[9]+1]='5' && buf[offsets[9]+2]> '2')) DayLight = -7;
  263.     if(buf[offsets[9]]='1' && (buf[offsets[9]+1]='1' && buf[offsets[9]+2]<'1')) DayLight = -7;
  264.     if(buf[offsets[9]]='0' && buf[offsets[9]+1]>'5') DayLight = -7;
  265.     if(buf[offsets[9]]='1' && buf[offsets[9]+1]<'1') DayLight = -7;
  266.  
  267.     if ((atoi(buf2) + DayLight) < 0)
  268.       j = atoi(buf2) + (24+ DayLight);
  269.     else
  270.       j=atoi(buf2) + DayLight;
  271. #if DEBUG == 1
  272.  
  273.  //Serial.print(atoi(buf2));
  274.  //Serial.print(" <- buf2   j-> ");
  275.  //Serial.println(j);
  276. #endif
  277.     j=sprintf(buf3,"%2.0d:%c%c",j,buf[offsets[1] + 2],buf[offsets[1] + 3]);
  278.     lcd.print(buf3);
  279.     ////Serial.print(buf3);
  280.     ////Serial.println(" <- buf3");
  281.     lcd.setCursor(13, 1);
  282.     j=sprintf(buf3," %c%c",buf[offsets[1] + 4],buf[offsets[1] + 5]);
  283.     j=atoi(buf3);
  284.     lcd.print(buf3);
  285.     //lcd.print(buf[offsets[1] + 5]);
  286. /*    
  287.  *     if (buf[offsets[2]] == 'V') {
  288.       lcd.setCursor(0, 1);
  289.       lcd.blink();
  290.       //lcd.print("    DATA VOID   ");
  291.       //Serial.println("    DATA VOID   ");
  292.       VOID = 1;
  293.       return 0;
  294.     } else {
  295.       if (VOID == 1) lcd.clear();
  296.       VOID = 0;
  297.       lcd.noBlink();
  298.       lcd.print(":");
  299.     }
  300. */
  301.  
  302.     // print Speed
  303.     for (j = 0; buf[offsets[7] + j] != ','; j++) {
  304.       buf2[j] = buf[offsets[7] + j];
  305.     }
  306.     buf2[j] = 0;
  307.     lcd.setCursor(0, 0);
  308.     f = atoi(buf2);
  309. //    if (f > 0) {
  310.       dtostrf(f*1.151,4,1,buf2);
  311.       if (atof(buf2)*1.151 > 75) {
  312.         lcd.blink();
  313.         //Serial.println("OVER SPEED BLINK ");
  314.       } else lcd.noBlink();
  315.       lcd.print(buf2);
  316.       lcd.print(" M ");
  317.       lcd.setCursor(0, 1);
  318.       dtostrf(f*1.852,4,1,buf2);
  319.       lcd.print(buf2);
  320.       lcd.print(" k");
  321. #if DEBUG == 1
  322.       //Serial.print(" SPEEEEDDDDD                          ");
  323.       //Serial.println(buf2);
  324. #endif
  325. /*    } else {
  326.       lcd.print("Stopped ");
  327.       lcd.setCursor(0, 1);
  328.       lcd.print("       ");
  329.     }
  330. */
  331.     // print heading
  332.     if (get_size(8) > 0) {
  333.       for (j = 0; buf[offsets[8] + j] != ','; j++) {
  334.         buf2[j] = buf[offsets[8] + j];
  335.       }
  336.       buf2[j] = 0;
  337.       lcd.setCursor(9, 1);
  338.       float f;
  339.       #if DEBUG == 0
  340.       Serial.print(buf2);
  341.       #endif
  342.       f = atof(buf2);
  343.       #if DEBUG == 0
  344.       Serial.print(" heading ");
  345.       Serial.println(f);
  346.       #endif
  347.       if(f >= 337.5 || f < 22.5) {
  348.         j=sprintf(buf2, "N  ");
  349.         h=0;
  350.       } else if(f >=    22.5 && f < 67.5) {
  351.         j=sprintf(buf2, "NE ");
  352.         h=1;
  353.       } else if (f >= 67.5 && f < 112.5) {
  354.         j=sprintf(buf2, "E  ");
  355.         h=2;
  356.       } else if (f >= 112.5 && f < 157.5) {
  357.         j=sprintf(buf2, "SE ");
  358.         h=3;
  359.       } else if (f >= 157.5 && f < 202.5) {
  360.         j=sprintf(buf2, "S");
  361.         h=4;
  362.       } else if (f >= 202.5 && f < 247.5) {
  363.         j=sprintf(buf2, "SW   ");
  364.         h=5;
  365.       } else if (f >= 247.5 && f < 292.5) {
  366.         j=sprintf(buf2, "W  ");
  367.         h=6;
  368.       } else if (f >= 292.5 && f < 337.5) {
  369.         j=sprintf(buf2, "NW   ");
  370.         h=7;
  371.       }
  372.       #if DEBUG == 0
  373.       Serial.print(" Buf2 ");
  374.       Serial.println(buf2);
  375.       #endif
  376.       lcd.print(buf2);
  377.       lcd.setCursor(12,1);
  378.       lcd.write(byte(h));
  379.       //Serial.print(buf2);
  380.     //  lcd.print("D");
  381.  
  382.     }
  383.     buf2[0] = 0;
  384.     counter2 = 0;
  385.     counter1 = 0;
  386.     //    offset=0;
  387.     offsets[0] = 0;
  388.     buf[0] = '*';
  389.     return 0;
  390.   } else {
  391.     return 0;
  392.   }
  393. }
  394.  
  395. int DoGPVTG(void ) {
  396.   char cmd[7]="$GPVTG";
  397.   int j,h=0;
  398.   char buf2[11];
  399.   char buf3[11];
  400.   for (j = 0; j < 6; j++) {
  401.     if (buf[j] != cmd[j]) {
  402.       return 0;
  403.     }
  404.   }
  405.   return 0;
  406. }
  407.  
  408. int DoGPGGA(void ) {
  409.   char cmd[7]="$GPGGA";
  410.   int j,h=0;
  411.   char buf2[11];
  412.   char buf3[11];
  413.     for (j = 0; j < 6; j++) {
  414.       if (buf[j] != cmd[j]) {
  415.         return 0;
  416.       }
  417.     }
  418.   return 0;
  419. }
  420.  
  421. int DoGPGSV(void ) {
  422.   char cmd[7]="$GPGSV";
  423.   int j,h=0;
  424.   char buf2[11];
  425.   char buf3[11];
  426.     for (j = 0; j < 6; j++) {
  427.       if (buf[j] != cmd[j]) {
  428.         return 0;
  429.       }
  430.     }
  431.   for (j = 1; buf[ j-1] != '*'; ++j) {
  432.     buf2[j] = buf[ j];
  433.   }
  434.   buf2[0] = '$';
  435.   buf2[j]=0;
  436. #if DEBUG > 0
  437.   Serial.println(buf2);
  438.   Serial.print(cmd);
  439.   Serial.print(" Satellites in view");
  440. #endif
  441.   lcd.setCursor(7,0);
  442.   for (j = 0; buf[offsets[3]+j]!=','; j++) {
  443.     lcd.print(buf[offsets[3]+j]);
  444.   }
  445.   lcd.print("S ");
  446.   return 0;
  447. }
  448.  
  449. int handle_byte(int byteGPS) {
  450.   if (byteGPS == '$') {
  451.     //offsets[12] = counter1;
  452.     counter1 = 0;
  453.     counter2 = 0;
  454.     offsets[0] = 0;
  455.     buf[1] = 0;
  456.   }
  457.   buf[counter1] = byteGPS;
  458.   buf[counter1 + 1] = 0;
  459.   buf[counter1 + 2] = 0;
  460.   counter1++;
  461.   if (counter1 == 300) {
  462.     buf[0] = 0;
  463.     buf[40] = 0;
  464.     buf[80] = 0;
  465.     return 0;
  466.   }
  467.   if (byteGPS == ',') {
  468.     counter2++;
  469.     offsets[counter2] = counter1;
  470.   }
  471.  
  472.   if (byteGPS == '*') {
  473.     if (buf[counter1 - 2] == 'V') {
  474.       VOID = 1;
  475.     }
  476.     buf[counter1 + 1] = 0;
  477.     buf[counter1 + 2] = 0;
  478.     buf[counter1 + 3] = 0;
  479.     buf[counter1 + 4] = 0;
  480.   }
  481.  
  482.   // Check if we got a <LF>, which indicates the end of line
  483.   if (byteGPS == 10) {
  484.     int h = 0;
  485.     buf[counter1 - 1] = 0;
  486.     buf[counter1] = 0;
  487.     buf[counter1 + 1] = 0;
  488.     buf[counter1 + 2] = 0;
  489.  
  490.     DoGPRMC();
  491.     DoGPVTG();
  492.     DoGPGSV();
  493.     DoGPGGA();
  494.     #if DEBUG == 2
  495.     Serial.println(buf);
  496.     #endif
  497.   }
  498.   return 1;
  499. }
  500.  
  501.  
  502. /**
  503.    Main loop
  504. */
  505. void loop() {
  506.   if (mySerial.available() > 0) {
  507.     byteGPS = mySerial.read();
  508. #if DEBUG > 5
  509.       Serial.print(byteGPS);
  510.     // Read a byte of the serial port
  511. #endif
  512.     //delay(50);
  513.     if (!handle_byte(byteGPS)) {
  514.       reset();
  515.     }
  516.   }
  517. Key = analogRead(0);
  518. if(abs(LastKey-Key)<20) {
  519.   if(++KeyCount>3) {
  520.     if(abs(Key-KeyUp)<30 ) {
  521.       if(backLight<251) backLight += 5;
  522.       KeyCount=0;
  523.       //Serial.println(backLight);
  524.     } else if(abs(Key-KeyDown)<30 ) {
  525.       if(backLight>4) backLight -= 5;
  526.       KeyCount=0;  
  527.       //Serial.println(backLight);
  528.     }
  529.   } // else KeyCount++;
  530. }else {
  531.   KeyCount=0;
  532.   if(Key<800) {
  533.     //Serial.print("LastKey " );
  534.     //Serial.print(LastKey);
  535.     //Serial.print(" Key ");
  536.     //Serial.println(Key);
  537.   }
  538. }
  539. LastKey=Key;
  540.   analogWrite(BackLightPin,backLight);
  541.   #if DEBUG > 3
  542.   Serial.print("BackLight  : ");
  543.   Serial.println(backLight);
  544.   #endif
  545. // int backLight=128;
  546. //int BackLightPin = 10;
  547.  
  548.  
  549.  
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement